﻿using DataHelper;
using Microsoft.Office.Interop.Word;
using SelfServiceWriteSystem;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HandyControlProject5
{
    /// <summary>
    /// DocView.xaml 的交互逻辑
    /// </summary>
    public partial class DocView : UserControl, INotifyPropertyChanged
    {

        MainWindow mainWindow;

        Timer timer;

        public DocView(MainWindow _mainWindow)
        {
            InitializeComponent();

            DataContext = this;
            mainWindow = _mainWindow;

            dsoFramer1.Toolbars = false;
            dsoFramer1.Menubar = false;
            dsoFramer1.Titlebar = false;
            //dsoFramer1.BorderStyle = DSOFramer.dsoBorderStyle.dsoBorderNone;

            

            dsoFramer2.Toolbars = false;
            dsoFramer2.Menubar = false;
            dsoFramer2.Titlebar = false;
            //dsoFramer2.BorderStyle = DSOFramer.dsoBorderStyle.dsoBorderNone;

            timer = new Timer(100);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();

         
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
           
            try
            {
                Document doc1 = dsoFramer1.ActiveDocument as Document;
                Document doc2 = dsoFramer2.ActiveDocument as Document;
                doc2.ActiveWindow.ActivePane.VerticalPercentScrolled = doc1.ActiveWindow.ActivePane.VerticalPercentScrolled;                
            }
            catch  { }
        }

        Project project;

        bool isFromSearch;

        public void GetDoc(long id,string name, bool isFromSearch)
        {
            try
            {
                dsoFramer1.Close();
                dsoFramer2.Close();
            }
            catch { }
            this.isFromSearch = isFromSearch;
            try
            {
                project = DbHelper.GetProject(id);


                LabelName.Text = "当前事项：";
                LabelText.Text = project.Name;
      
                //https://www.cnblogs.com/sntetwt/p/5401777.html
                string basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                //string docFile = basePath + @"doc\"+ id .ToString()+ ".doc";
                string docFile = basePath  + project.DocEmpty;
                /*
                string pdfFile = basePath + @"pdf\" + id.ToString() + ".pdf";
                if(!File.Exists(pdfFile))
                {
                    WordToPdfHelper wordToPdfHelper = new WordToPdfHelper();
                    wordToPdfHelper.ToPdf(docFile, pdfFile);
                }
                */
                //theBrowser1.Address = "file:///" + pdfFile.Replace('\\', '/'); ;

                File.Copy(docFile, basePath + "temp20200919.doc", true);

                OpenDocTemplate(basePath + project.DocTemplate);
                if (project.DocTemplate != project.DocEmpty)
                    OpenDocEmpty(basePath + "temp20200919.doc");

            }
            catch (Exception ex)
            {
                MessageBox.Show("错误信息：" + ex.Message,"出错了！");
            }
        }

        public void OpenDocTemplate(string docFile)
        {
            dsoFramer1.Close();
            try
            {
                if (docFile.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase)
                    || docFile.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.dsoFramer1.Open(docFile, false, "Excel.Sheet", null, null);
                }
                else if (docFile.EndsWith(".pptx", StringComparison.InvariantCultureIgnoreCase)
                    || docFile.EndsWith(".ppt", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.dsoFramer1.Open(docFile, false, "PowerPoint.Show", null, null);
                }
                else if (docFile.EndsWith(".docx", StringComparison.InvariantCultureIgnoreCase)
                    || docFile.EndsWith(".doc", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.dsoFramer1.Open(docFile, false, "Word.Document", null, null);
                    Document doc1 = dsoFramer1.ActiveDocument as Document;
                    doc1.Protect(WdProtectionType.wdAllowOnlyReading);
                }
            }
            catch (System.Runtime.InteropServices.COMException come)
            {
                //MessageBox.Show(come.Message);
            }
        }
        public void OpenDocEmpty(string docFile)
        {
            dsoFramer2.Close();
            try
            {
                if (docFile.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase)
                    || docFile.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.dsoFramer2.Open(docFile, false, "Excel.Sheet", null, null);
                }
                else if (docFile.EndsWith(".pptx", StringComparison.InvariantCultureIgnoreCase)
                    || docFile.EndsWith(".ppt", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.dsoFramer2.Open(docFile, false, "PowerPoint.Show", null, null);
                }
                else if (docFile.EndsWith(".docx", StringComparison.InvariantCultureIgnoreCase)
                    || docFile.EndsWith(".doc", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.dsoFramer2.Open(docFile, false, "Word.Document", null, null);
                }
            }
            catch (System.Runtime.InteropServices.COMException come)
            {
                //MessageBox.Show(come.Message);
            }
        }
     

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(isFromSearch)
                mainWindow.UserContent = mainWindow.searchList;
            else
                mainWindow.UserContent = mainWindow.mainList;
        }

        private void dsoFramer2_OnActivationChange(object sender, AxDSOFramer._DFramerCtlEvents_OnActivationChangeEvent e)
        {
            Console.WriteLine("dsoFramer2_OnActivationChange");

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            dsoFramer2.Close();
            File.Copy(basePath + project.DocEmpty, basePath + "temp20200919.doc",true);     
            if (project.DocTemplate != project.DocEmpty)
                OpenDocEmpty(basePath + "temp20200919.doc");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //dsoFramer2.Save("temp20200909.doc", true, "", "");
            //PrintHelper.PrintFile("temp20200909.doc");
            dsoFramer2.PrintOut();
            //Document doc2 = dsoFramer2.ActiveDocument as Document;
            //doc2.PrintOut();
        }

        private void dsoFramer1_OnActivationChange(object sender, AxDSOFramer._DFramerCtlEvents_OnActivationChangeEvent e)
        {
            Console.WriteLine("dsoFramer1_OnActivationChange");
        }
    }
}
